package com.xiran.todoself.mapper;


import com.xiran.todoself.model.pojo.TodoItem;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Mapper
public interface TodoItemMapper {
    @Select("select * from todo_item where user_id = #{userId} ")
    List<TodoItem> listByUser(Long userId);


    @Update("update todo_item set completed = #{completed}, finish_time = #{finishTime} where id = #{id} and user_id = #{userId}")
    int updateStatus(
            @Param("id") Long id,
            @Param("userId") Long userId,
            @Param("completed") Boolean completed,
            @Param("finishTime") LocalDateTime finishTime
    );

    @Delete("delete from todo_item where id = #{id} and user_id = #{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);


    @Select("SELECT * FROM todo_item WHERE id = #{id} AND user_id = #{userId}")
    TodoItem findByIdAndUser(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT * FROM todo_item WHERE id = #{id}")
    TodoItem findById(Long id);

    @Select("SELECT LAST_INSERT_ID()")
    Long lastInsertId();

    @Insert("insert into todo_item(user_id, title, create_time) values(#{todoItem.userId}, #{todoItem.title}, #{todoItem.createTime})")
    void insert(@Param("todoItem") TodoItem todoItem);


}
